﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace WeatherBar2
{
    class Helper
    {
        public static Dictionary<string, string> GetData(string location)
        {
            int forecastDays = 0;
            Dictionary<string, string> data = new Dictionary<string, string>();
            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(string.Format("http://www.google.com/ig/api?weather={0}", location));

                if (document.SelectSingleNode("/xml_api_reply/weather/problem_cause") == null)
                {
                    // General forecast information

                    data.Add("location", document.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText);
                    data.Add("datetime", document.SelectSingleNode("/xml_api_reply/weather/forecast_information/current_date_time").Attributes["data"].InnerText);
                    data.Add("unitsys", document.SelectSingleNode("/xml_api_reply/weather/forecast_information/unit_system").Attributes["data"].InnerText);

                    // Current conditions
                    data.Add("current_condition", document.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition").Attributes["data"].InnerText);
                    data.Add("current_tempf", document.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_f").Attributes["data"].InnerText);
                    data.Add("current_tempc", document.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes["data"].InnerText);
                    data.Add("current_humidity", document.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes["data"].InnerText);
                    data.Add("current_wind", document.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition").Attributes["data"].InnerText);

                    // Forecast conditions
                    foreach (XmlNode node in document.SelectNodes("/xml_api_reply/weather/forecast_conditions"))
                    {
                        data.Add("forecast_day" + forecastDays.ToString() + "_dow", node.SelectSingleNode("day_of_week").Attributes["data"].InnerText);
                        data.Add("forecast_day" + forecastDays.ToString() + "_low",node.SelectSingleNode("low").Attributes["data"].InnerText);
                        data.Add("forecast_day" + forecastDays.ToString() + "_high", node.SelectSingleNode("high").Attributes["data"].InnerText);
                        data.Add("forecast_day" + forecastDays.ToString() +"_condition", node.SelectSingleNode("condition").Attributes["data"].InnerText);
                        forecastDays++;
                    }
                }
                else
                {
                    MessageBox.Show("An error occured while obtaining the weather conditions.\nUnknown location.",
                        "WeatherBar 2",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured while obtaining weather conditions.\n" + ex.Message,
                    "WeatherBar 2", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            return data;
        }
    }
}
